package org.lex.swing.prefs.old;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Arrays;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import org.lex.swing.ContainerFactory;
import org.lex.swing.prefs.CategoriesModel;
import org.lex.swing.prefs.Category;
import org.lex.swing.prefs.FontCategory;
import org.lex.swing.widgets.EnhancedDialog;


public class ListPreferencesDialog extends EnhancedDialog {
	public static final String DEFAULT_TITLE = "Preferences";
	private Category[] allCategories = new Category[0];
	private JPanel controlPane = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JPanel categoryPane = null;
	private CardLayout categoryPaneCardLayout = new CardLayout();
	private JPanel pane = null;
	private JSplitPane mainSplitPane = null;

	public static void main(String[] args) {
		ListPreferencesDialog dialog = new ListPreferencesDialog((JFrame) null);
		dialog.setLocationRelativeTo(null);
		dialog.loadCategories(new FontCategory());
		dialog.setVisible(true);
	}

	public ListPreferencesDialog() {
		this((Window) null);
	}

	public ListPreferencesDialog(String title) {
		this(null, title);
	}

	public ListPreferencesDialog(Window parent) {
		this(parent, DEFAULT_TITLE);
	}

	public ListPreferencesDialog(Window parent, String title) {
		super(parent, title, true);
		this.initialize();
	}

	private CategoriesModel categoriesList = null;

	protected CategoriesModel getCategoriesListPane() {
		if (null == categoriesList) {
			categoriesList = new ListCategoriesPane();
			categoriesList.addCategoryChangedListener(new PropertyChangeListener() {
				public void propertyChange(PropertyChangeEvent e) {
					categoryPaneCardLayout.show(categoryPane, e.getPropertyName());
				}
			});
		}
		return this.categoriesList;
	}

	public void loadCategories(Category... categories) {
		if (null == categories)
			return;
		this.getCategoriesListPane().addCategories(categories);
		for (Category category : categories) {
			category.loadConfiguration();
			String title = category.getTitle();
			this.getCategoryPane().add(category.getCategoryComponent(), title);
		}
		this.allCategories = org.lex.utils.Arrays.append(allCategories, categories);
		Arrays.sort(allCategories, Category.comparator);
	}

	public void saveConfiguration() {
		// �Ȱ��޸ĺ���������ö�ӳ�䵽Configuration��
		for (Category category : this.allCategories)
			category.saveConfiguration();
		// Ȼ������Ӧ�ø�����������������ñ��浽ָ���ĵط�
	}

	public void cancel() {
		if (false == this.hasConfigurationChanged()) {
			this.dispose();
			return;
		}
		int opt = JOptionPane.showConfirmDialog(this, "Do you want to save the changes?", "Save", JOptionPane.YES_NO_CANCEL_OPTION,
				JOptionPane.QUESTION_MESSAGE);
		if (JOptionPane.YES_OPTION == opt) {
			this.saveConfiguration();
			dispose();
		} else if (JOptionPane.NO_OPTION == opt) {
			dispose();
		} else if (JOptionPane.CANCEL_OPTION == opt) {
			// return to PreferencesDialog
		}
	}

	/**
	 * �����Ƿ���ı�
	 * 
	 * @return
	 */
	private boolean hasConfigurationChanged() {
		if (null == this.allCategories)
			return false;
		for (Category c : this.allCategories)
			if (true == c.hasContentChanged())
				return true;
		return false;
	}

	private void initialize() {
		this.setSize(500, 400);
		this.setTitle("Preferences");
		this.setModal(true);
		this.setContentPane(getPane());
		this.getRootPane().setDefaultButton(this.getOkButton());
	}

	private JPanel getPane() {
		if (pane == null) {
			pane = new JPanel();
			pane.setLayout(new BorderLayout());
			pane.add(Box.createVerticalStrut(11), BorderLayout.NORTH);
			pane.add(Box.createHorizontalStrut(11), BorderLayout.WEST);
			pane.add(Box.createHorizontalStrut(12), BorderLayout.EAST);
			pane.add(getMainSplitPane(), BorderLayout.CENTER);
			pane.add(getControlPane(), BorderLayout.SOUTH);
		}
		return pane;
	}

	private JSplitPane getMainSplitPane() {
		if (mainSplitPane == null) {
			mainSplitPane = ContainerFactory.createBorderlessSplitPane();
			mainSplitPane.setRightComponent(getCategoryPane());
			mainSplitPane.setLeftComponent(getCategoriesListPane());
			mainSplitPane.setResizeWeight(0.25D);
			mainSplitPane.setDividerLocation(100);
		}
		return mainSplitPane;
	}

	private JPanel getCategoryPane() {
		if (categoryPane == null) {
			categoryPane = new JPanel();
			categoryPane.setLayout(this.categoryPaneCardLayout);
		}
		return categoryPane;
	}

	private JPanel getControlPane() {
		if (controlPane == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(FlowLayout.TRAILING);
			flowLayout.setHgap(11);
			flowLayout.setVgap(11);
			flowLayout.setAlignOnBaseline(true);
			controlPane = new JPanel(flowLayout);
			controlPane.add(getOkButton(), null);
			controlPane.add(getCancelButton(), null);
		}
		return controlPane;
	}

	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (true == hasConfigurationChanged())
						saveConfiguration();
					dispose();
				}
			});
		}
		return okButton;
	}

	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					cancel();
				}
			});
		}
		return cancelButton;
	}
}
